package org.example.mybatis.mapper;

import org.apache.ibatis.annotations.*;
import org.example.mybatis.model.UserInfo;

import java.util.List;

/**
 * 访问数据库接口
 */

@Mapper
public interface UserInfoMapper {
    /**
     * 返回数据列表
     */
    @Select("select id , username , password, age , gender , phone , " +
            "delete_flag as deleteFlag" +
            " , create_time as createTime," +
            " update_time as updateTime " +
            " from userinfo")   //查询操作,使用select, 并且写入 sql ;
    List<UserInfo> queryUserInfoList() ;

    @Results( id = "BaseResult"
            , value = {
            @Result(column = "delete_flag" , property = "deleteFlag"),
            @Result(column = "create_time" , property = "createTime"),
            @Result(column = "update_time" , property = "updateTime"),
    }
    )
    @Select("select * from userinfo where id=#{param1} or delete_flag=#{param2} ")
    List<UserInfo> queryUserInfo(Integer userId , Integer delete_flag);

//    @ResultMap(value = "BaseResult")
//    @Select("select * from userinfo where id=#{id} or delete_flag=#{delete_flag} ")
//    List<UserInfo> queryUserInfo2(@Param("id") Integer userId ,@Param("delete_flag") Integer delete_flag);

    @Select("select * from userinfo where id=#{id} or delete_flag=#{delete_flag} ")
    List<UserInfo> queryUserInfo2(@Param("id") Integer userId ,@Param("delete_flag") Integer delete_flag);


    @Options(useGeneratedKeys = true , keyProperty = "id")
    @Insert("insert into userinfo(username , password , age, phone) " +
            " values (#{userInfo.username},  #{userInfo.password},#{userInfo.age} , #{userInfo.phone})")
    Integer insertUserInfo(@Param("userInfo") UserInfo userInfo);

    @Delete("delete from userinfo where id=#{id}")
    Integer deleteUserInfo(@Param("id")Integer userId);

    @Update("update userinfo set age=#{age},phone=#{phone},password=#{password} where id=#{id}")
    Integer updateInfo(UserInfo userInfo);

    @Select("select * from userinfo where id=#{id}")
    List<UserInfo> queryUserinfoForId(@Param("id") Integer userId);

    @Select("select * from userinfo where username= '${username}' and password= '${password}' ")
    List<UserInfo> queryUserInfoByUserNameAndPassword
            (@Param("username") String username,@Param("password") String password);
    @Select("select * from userinfo order by id ${by};")
    List<UserInfo> queryOrder(@Param("by") String order);

    @Select("SELECT * FROM `userinfo` WHERE username LIKE CONCAT( '%',#{name},'%')")
    List<UserInfo> queryLike(@Param("name") String username);

}
